package com.cmccgz.udacs_landlord_webapp.main.web;

import android.annotation.TargetApi;
import android.graphics.Bitmap;
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.cmccgz.baselibrary.base.BaseActivity;
import com.cmccgz.udacs_landlord_webapp.R;
import com.cmccgz.udacs_landlord_webapp.bean.WebBean;
import com.cmccgz.udacs_landlord_webapp.utils.ViewHelper;


public class WebActivity extends BaseActivity implements View.OnClickListener{
    private WebBean webBean;
    private WebView wv;
    private TextView tv_fail;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_web;
    }

    @Override
    protected void initView() {
        webBean = ((WebBean) getIntent().getSerializableExtra("webBean"));

        ViewHelper instance = ViewHelper.instance(this);
        instance.setBackTextVisable(true);
        instance.setTitle(webBean.title);

        wv = ((WebView) findViewById(R.id.wv));
        tv_fail = ((TextView) findViewById(R.id.tv_fail));

        wv.setWebViewClient(new WebViewClient(){
            @TargetApi(Build.VERSION_CODES.LOLLIPOP)
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                view.loadUrl(request.getUrl().getPath());
                return true;
            }
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO Auto-generated method stub
                // return super.shouldOverrideUrlLoading(view, url);
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                // TODO Auto-generated method stub
                tv_fail.setVisibility(View.GONE);
                dismissLoadingDialog();
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                showLoadingDialog("正在加载网页...");
            }

            @Override
            public void onReceivedError(WebView view, int errorCode,
                                        String description, String failingUrl) {
                // TODO Auto-generated method stub
                super.onReceivedError(view, errorCode, description, failingUrl);
                tv_fail.setVisibility(View.VISIBLE);
                dismissLoadingDialog();
            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
                tv_fail.setVisibility(View.VISIBLE);
                dismissLoadingDialog();
            }
        });
        wv.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if (newProgress==100) {
                    dismissLoadingDialog();
                }
                Log.e("inx", "onProgressChanged: "+newProgress );
            }
        });
        WebSettings settings = wv.getSettings();
        settings.setJavaScriptEnabled(true);
        //支持屏幕缩放
        settings.setSupportZoom(true);
        settings.setBuiltInZoomControls(true);
        settings.setDefaultTextEncodingName("utf-8");
        settings.setAppCacheEnabled(true);// 设置启动缓存
        settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        wv.setInitialScale(100);
        wv.loadUrl(webBean.webUrl);
    }

    @Override
    protected void initFragment() {

    }

    @Override
    protected void beforePermission() {

    }

    @Override
    protected int setSystemBarColorId() {
        return 0;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_back:
                onBackPressed();
                break;
        }
    }
}
